<template>
  <div class="box">
    <!-- 顶部搜索区域 -->
    <el-card>
      <el-form v-model="searchDate" ref="searchRef" label-width="100px" class="demo-ruleForm">
        <el-row class="demo-ruleForm-row">
          <el-col :span="6">
            <el-form-item label="项目名称" prop="name">
              <el-input placeholder="请输入招标项目名称" v-model="searchDate.name" clearable></el-input>
            </el-form-item>
          </el-col>
           <el-col :span="6">
            <el-form-item label="项目负责人" prop="subcontract">
              <el-input placeholder="请输入项目负责人名称" v-model="searchDate.subcontract" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="招标编号" prop="company_name">
              <el-input placeholder="请输入招标编号" v-model="searchDate.company_name" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4" >
            <div class="search_col">
              <el-button type="primary" size="mini" icon="el-icon-search">搜索</el-button>
              <el-button type="primary" size="mini" icon="el-icon-refresh" @click="resetForm">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

     <!-- 卡片视图区域 -->
    <el-card class="card">
      <el-row>
        <el-col :span="6">
          <el-button icon="el-icon-files" type="primary" size="mini" @click="upDialogVisible = true">导入中标结果表</el-button>
        </el-col>
      </el-row>
      <el-table
      :data="tableDate"
      :row-class-name="tableRowClassName"
      style="width: 100%"
      border
      ref="refTable"
      :header-cell-style="{
        'background-color': '#CBD9EC',
        color: '#fff',
        'font-weight': '400',
      }"
      @selection-change="handleSelectionChange">
        <!-- <el-table-column
          type="selection"
          width="55">
        </el-table-column> -->
        <el-table-column width="60" label="序号" type="index"></el-table-column>
        <el-table-column prop="name" label="招标项目名称" width="260"></el-table-column>
        <el-table-column prop="info_number" label="招标编号" width="240"> </el-table-column>
        <el-table-column prop="announcement" label="公告发布时间" width="200"> </el-table-column>
        <el-table-column prop="time" label="开标时间" width="200"> </el-table-column>
        <el-table-column prop="region" label="地区" width="200"></el-table-column>
        <el-table-column prop="procurement_people" label="采购人" width="120"></el-table-column>
        <el-table-column prop="procurement_contact" label="采购联系人" width="120"> </el-table-column>
        <el-table-column prop="institutions" label="代理机构" width="120"> </el-table-column>
        <el-table-column prop="responsibility" label="项目负责人" width="120"></el-table-column>
        <el-table-column prop="mobile" label="联系方式" width="150"></el-table-column>
        <el-table-column prop="procurement_way" label="采购方式"></el-table-column>
        <el-table-column prop="money" label="预算/拟分包金额（万元）" width="130"></el-table-column>
        <el-table-column label="操作" class="operation" width="100px">
          <template>
            <el-row>
              <el-button type="primary" size="mini" @click="checkClick">查看</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.mount"
        :page-sizes="[1, 3, 6, 10]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 上传弹框 -->
    <el-dialog title="表格数据上传" :visible.sync="upDialogVisible" width="60%" @close="upDialogVisibleClosed">
      <el-form :model="form">
       <el-form-item label="上传文件"  :label-width="formLabelWidth">
          
        <el-upload class="upload-demo"
        :on-error="errorClick"
        :on-remove="removeFile"
        :on-success="handleSuccess"
        :before-upload="beforeUploadFile"
        :on-change="fileChange"
         ref="uploadExcel" :auto-upload="false" action="http://192.168.5.197:8040/PM/fileUpload" drag name="fileName" >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip"></div>
        </el-upload>
        
       </el-form-item>
     </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button  @click="deleteClicks">取 消</el-button>
          <el-button type="primary" @click="submitUpload">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 搜索数据
      searchDate: {
        name: '',
        subcontract: '',
        company_name: ''
      },
       // 第一个表格
      tableDate: [
        {
          id: 1,
          name: '国网江西省电力有限公司上饶供电分公司集体企业2020年第002批物资类招标',
          info_number: 'JXRY2020-SR-002-JT(WZ)',
          announcement: '公告',
          time: '2020年6月6日下午16:30时',
          region: '地区',
          procurement_people: '采购人',
          procurement_contact: '采购联系人',
          institutions: '机构1',
          responsibility: '项目负责人',
          mobile: '联系方式',
          procurement_way: '采购方式',
          money: 383.46696,
          audit_status: 0,
        },
        {
          id: 2,
          name: '国网江西省电力有限公司上饶供电分公司集体企业2020年第002批物资类招标',
          info_number: 'JXRY2020-SR-002-JT(WZ)',
          announcement: '公告',
          time: '2020年6月6日下午16:30时',
          region: '地区',
          procurement_people: '采购人',
          procurement_contact: '采购联系人',
          institutions: '机构2',
          responsibility: '项目负责人',
          mobile: '联系方式',
          procurement_way: '采购方式',
          money: 383.46696,
          audit_status: 1,
        },
        {
          id: 3,
          name: '国网江西省电力有限公司上饶供电分公司集体企业2020年第002批物资类招标',
          info_number: 'JXRY2020-SR-002-JT(WZ)',
          announcement: '公告',
          time: '2020年6月6日下午16:30时',
          region: '地区',
          procurement_people: '采购人',
          procurement_contact: '采购联系人',
          institutions: '机构3',
          responsibility: '项目负责人',
          mobile: '联系方式',
          procurement_way: '采购方式',
          money: 383.46696,
          audit_status: 2,
        },
        {
          id: 3,
          name: '国网江西省电力有限公司上饶供电分公司集体企业2020年第002批物资类招标',
          info_number: 'JXRY2020-SR-002-JT(WZ)',
          announcement: '公告',
          time: '2020年6月6日下午16:30时',
          region: '地区',
          procurement_people: '采购人',
          procurement_contact: '采购联系人',
          institutions: '机构3',
          responsibility: '项目负责人',
          mobile: '联系方式',
          procurement_way: '采购方式',
          money: 383.46696,
          audit_status: 3,
        },
        {
          id: 5,
          name: '国网江西省电力有限公司上饶供电分公司集体企业2020年第002批物资类招标',
          info_number: 'JXRY2020-SR-002-JT(WZ)',
          announcement: '公告',
          time: '2020年6月6日下午16:30时',
          region: '地区',
          procurement_people: '采购人',
          procurement_contact: '采购联系人',
          institutions: '机构1',
          responsibility: '项目负责人',
          mobile: '联系方式',
          procurement_way: '采购方式',
          money: 383.46696,
          audit_status: 0,
        },
      ],

      // 分页
      page: {
        // 每页显示几条数据
        size: 6,
        // 当前页码值
        mount: 0,
      },
      // 总数据条数
      total: 6,

      // 存储选中的数据
      checkboxList: [],

      upDialogVisible: false,
    }
  },
  methods: {
    // 隔行变色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },

    // 重置事件
    resetForm() {
      this.searchDate.name = '';
      this.searchDate.subcontract = '';
      this.searchDate.company_name = '';
    },

    // 文件上传成功时的钩子
    handleSuccess(res, file, fileList) {
      // alert(file + '2');
      // 上传成功之后，清空form.file数组
      this.form.file = [];
      this.$notify.success({
        title: '成功',
        message: `文件上传成功`
      });
      // 隐藏 上传弹框
      this.upDialogVisible = false
    },
    // 文件状态改变时的钩子
    fileChange(file, fileList) {
      // fileList中存储了要上传的文件
      // 遍历fileList 
      for(var i = 0; i < fileList.length; i++) {
        // 给 form.file赋值
        this.form.file[i] = file.raw
      }
    },
     // 上传文件之前的钩子, 参数为上传的文件,若返回 false 或者返回 Promise 且被 reject，则停止上传
    beforeUploadFile(file) {
      let extension = file.name.substring(file.name.lastIndexOf('.')+1);
      // 文件大小
      let size = file.size / 1024 / 1024
      // 判断 文件后缀名是否是xlsx
      if(extension !== 'xlsx') {
        this.$notify.warning({
          title: '警告',
          message: `只能上传Excel2017（即后缀是.xlsx）的文件`
        });
        return false;
      }
      if(size > 10) {
        this.$notify.warning({
          title: '警告',
          message: `文件大小不得超过10M`
        });
        return false;
      }
    },
    // 删除文件事件
    removeFile(file, fileList) {
      // 删除文件，数组清空
      this.form.file = []; 
      // // 遍历 lileList
      for(var i = 0; i < fileList.length; i++) {
        // 重新在 form.lile中添加数据
        this.form.file[i] = fileList[i].raw
      }
      
    },
    // 点击取消事件
    deleteClicks() {
      console.log('已经取消上传');
      // 取消请求
      this.$refs.uploadExcel.abort();
      // 隐藏弹框
      this.upDialogVisible = false;
    },
    // 点击上传确认事件
    submitUpload(id) {
      // 手动上传文件，发送请求
      this.$refs.uploadExcel.submit();
      // let formData = new FormData()
      // formData.append('fileName', this.form.file);
      // console.log(this.form.file);
      // console.log(this.formData);
      // axios.post('http://192.168.5.197:8040/PM/fileUpload', 
      //   formData,
      //   { "Content-Type": "multipart/form-data" }
      // )
      // .then(res => {
      //   console.log('res')
      //   console.log(res)
      // })
      // .catch(err => {
      // })
    },
    // 文件上传失败时触发
    errorClick(err, file, fileList) {
      console.log('文件上传失败');
      console.log(err);
      // let myError=err.toString();//转字符串
      // myError=myError.replace("Error: ","") // 去掉前面的" Error: "
      // myError=JSON.parse(myError);//转对象
      // console.log(myError);
    },
    // 监听上传弹框是否隐藏
    upDialogVisibleClosed() {
      this.$refs.uploadExcel.clearFiles();
    },

    // 每页数据显示条数改变时触发
    handleSizeChange(newSize) {
      console.log(newSize)
    },
    // 页码值发生改变时触发
    handleCurrentChange(newPage) {
      console.log(newPage)
    },

    // 全选
    handleSelectionChange(val) {
      this.checkboxList = val;
      console.log(this.checkboxList);
    },

    // 详情事件区域
    // 点击详情事件触发
    detailsClick(index, row) {
      // console.log(row)
      this.companyData = row.children
      this.companyData.index = index
      // console.log(this.companyData)

      this.invoiceData = row.list
      this.invoiceData.index = index
      // console.log(this.invoiceData)
      this.detailsDialogVisible = true
    },
    // tab栏选中项改变时触发
    handleClick(tab, event) {
      // console.log(tab, event)
    },

    // 点击查看
    checkClick() {
      this.$router.push({path:`/nested/menu4`});
    },
  }
}
</script>

<style>
.box{
    padding: 20px;
}
</style>